package com.horzion.httpclient.controller;

import com.horzion.httpclient.dao.UserDao;
import com.horzion.httpclient.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    /**
     * 添加用户
     *
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping
    public Map save(@RequestBody User user) throws Exception {
        user.setBirthday(new Date());
        userDao.save(user);
        Map returnMap = new HashMap<>();
        returnMap.put("message", "添加成功");

        return returnMap;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Map delete(@PathVariable Integer id) {

        userDao.deleteById(id);

        Map returnMap = new HashMap<>();
        returnMap.put("message", "删除成功");
        return returnMap;
    }

    /**
     * 修改用户
     *
     * @param id
     * @param user
     * @return
     */
    @PutMapping("/{id}")
    public Map update(@PathVariable Integer id, @RequestBody User user) {

        user.setId(id);
        userDao.save(user);

        Map returnMap = new HashMap<>();
        returnMap.put("message", "修改成功");
        return returnMap;
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User findById(@PathVariable Integer id) {
        return userDao.findById(id).get();
    }

    /**
     * 查询全部用户
     *
     * @return
     */
    @GetMapping
    public List<User> findAll() {
        return userDao.findAll();
    }

/**
 * 上传头像
 *
 * @param pic
 * @param username
 * @return
 * @throws Exception
 */
@PostMapping("/upload")
// 如果上传了多个文件使用：List<MultipartFile> pic
public Map upload(MultipartFile pic, String username) throws Exception {

    pic.transferTo(new File("D:\\", UUID.randomUUID().toString() + ".png"));

    Map returnMap = new HashMap<>();
    returnMap.put("message", "上传成功");
    returnMap.put("data", username);
    return returnMap;
}


    @GetMapping("/testGet")
    public Map testGet(String username) throws Exception {

        Map returnMap = new HashMap<>();
        returnMap.put("message", "请求成功");
        returnMap.put("data", username);
        return returnMap;
    }


    @PostMapping("/testPost")
    public Map testPost() throws Exception {

        Map returnMap = new HashMap<>();
        returnMap.put("message", "普通方式post请求成功");
        return returnMap;
    }

    @PostMapping("/testPost2")
    public Map testPost2(String username) throws Exception {

        Map returnMap = new HashMap<>();
        returnMap.put("message", "普通方式post请求成功");
        returnMap.put("data", username);
        return returnMap;
    }


    @PostMapping("/testPost3")
    public Map testPost3(@RequestBody User user, String flag) throws Exception {
        user.setBirthday(new Date());
        userDao.save(user);
        Map returnMap = new HashMap<>();
        returnMap.put("message", "请求testPost3成功");
        returnMap.put("data", flag);

        return returnMap;
    }


    /**
     * 测试表单传输
     *
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/testForm")
    public Map testPost3(User user) throws Exception {
        Map returnMap = new HashMap<>();
        returnMap.put("message", "请求testForm成功");
        returnMap.put("data", user);

        return returnMap;
    }

/**
 * 测试数据流传输
 *
 * @param is
 * @return
 * @throws Exception
 */
@PostMapping(value = "/testStream")
public Map testStream(InputStream is) throws Exception {
    // 接受前端传递的数据
    String str = new BufferedReader(new InputStreamReader(is)).readLine();
    Map returnMap = new HashMap<>();
    returnMap.put("message", "testStream");
    returnMap.put("data", str);

    return returnMap;
}
}
